﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using DEMO3.GUI;
using DEMO3.BUS;
using DEMO3.DTO;

namespace DEMO3
{
    public partial class Main : Form
    {
        
        public Main()
        {
            InitializeComponent();
        }
        private Control Temp;
        private bool login_more = false;
        private void btDangNhap_Click(object sender, EventArgs e)
        {
            if (!login_more)
            {
                pnLogin.Height = 90;
                login_more = true;
            }
            else
            {
                pnLogin.Height = 40;
                login_more = false;
            }
        }

        private void lbXinchao_Click(object sender, EventArgs e)
        {
            btDangKi.Visible = true;
            btDangNhap.Visible = true;
            lbXinchao.Visible = false;
            login_more = false;
            txtID.Text = null; txtPW.Text = null;
            rankname = rank = "KH";
            Auth();
            pnMain.Controls.Remove(Temp);
        }

        private void txtPW_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                pictureBox1_Click(e, new EventArgs());
        }       

        public static void Warning()
        {
            MessageBox.Show("Vui lòng kiểm tra lại kết nối...","Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        protected override void OnLoad(EventArgs args)
        {
            base.OnLoad(args);
            Application.Idle += new EventHandler(OnLoaded);
        }

        private void OnLoaded(object sender,EventArgs args)
        {
            Application.Idle -= new EventHandler(OnLoaded);
            //TODO: ABC
        }
        private void CheckApplication()
        {
            txtLoading.Visible = true;
            if (b_connection)
            {
                pConnect.Image = Properties.Resources.connection_established;
                txtLoading.Text = "Kết nối dữ liệu thành công!!!";
            }
            else
            {
                pConnect.Image = Properties.Resources.disconnect;
                txtLoading.Text = "Kết nối dữ liệu thất bại :(";
            }
            Application.DoEvents();
            txtLoading.Visible = false;
            txtLoading.Text = "Loading...";
        }

        public static bool b_connection = false;
        private bool b_pre1 = false;
        private void Main_Load(object sender, EventArgs e)
        {            
            Main.txtLoading = new System.Windows.Forms.Label();
            // 
            // txtLoading
            // 
            DEMO3.Main.txtLoading.AutoSize = true;
            DEMO3.Main.txtLoading.Location = new System.Drawing.Point(301, 59);
            DEMO3.Main.txtLoading.Name = "txtLoading";
            DEMO3.Main.txtLoading.Size = new System.Drawing.Size(35, 13);
            DEMO3.Main.txtLoading.TabIndex = 4;
            DEMO3.Main.txtLoading.Text = "Loading...";
            DEMO3.Main.txtLoading.Visible = true;
            DEMO3.Main.txtLoading.BackColor = System.Drawing.Color.Transparent;
            DEMO3.Main.txtLoading.Font = new System.Drawing.Font("Calibri", 12F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            DEMO3.Main.txtLoading.ForeColor = System.Drawing.SystemColors.Highlight;
            this.panel4.Controls.Add(DEMO3.Main.txtLoading);
            DataProvider test = new DataProvider();
            test.OpenConnect(); test.CloseConnect();
            cbxLoaiID.SelectedIndex = 0;
            Auth();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (b_pre1 != b_connection)
            {
                CheckApplication();
                b_pre1 = b_connection;
            }
        }

        private void nav3_Click(object sender, EventArgs e)
        {
            txtLoading.Visible = true;
            Application.DoEvents();
            pnMain.Controls.Remove(Temp);
            Temp = new ThanhVien();
            Temp.Dock = DockStyle.Fill;
            pnMain.Controls.Add(Temp);
            Application.DoEvents();
            txtLoading.Visible = false;
        }

        private void nav4_Click(object sender, EventArgs e)
        {            
            txtLoading.Visible = true;
            Application.DoEvents();
            pnMain.Controls.Remove(Temp);
            Temp = new CaChieu();
            Temp.Dock = DockStyle.Fill;
            pnMain.Controls.Add(Temp);
            Application.DoEvents();
            txtLoading.Visible = false;
        }

        private void nav2_Click(object sender, EventArgs e)
        {
            txtLoading.Visible = true;
            Application.DoEvents();
            pnMain.Controls.Remove(Temp);
            Temp = new VeBan();
            Temp.Dock = DockStyle.Fill;
            pnMain.Controls.Add(Temp);
            Application.DoEvents();
            txtLoading.Visible = false;
        }

        private void nav5_Click(object sender, EventArgs e)
        {
            txtLoading.Visible = true;
            Application.DoEvents();
            pnMain.Controls.Remove(Temp);
            Temp = new SuatChieu();
            Temp.Dock = DockStyle.Fill;
            pnMain.Controls.Add(Temp);
            Application.DoEvents();
            txtLoading.Visible = false;
        }

        private void nav7_Click(object sender, EventArgs e)
        {
            txtLoading.Visible = true;
            Application.DoEvents();
            pnMain.Controls.Remove(Temp);
            Temp = new Phim();
            Temp.Dock = DockStyle.Fill;
            pnMain.Controls.Add(Temp);
            Application.DoEvents();
            txtLoading.Visible = false;
        }

        private void nav6_Click(object sender, EventArgs e)
        {
            txtLoading.Visible = true;
            Application.DoEvents();
            pnMain.Controls.Remove(Temp);
            Temp = new PhongChieu();
            Temp.Dock = DockStyle.Fill;
            pnMain.Controls.Add(Temp);
            Application.DoEvents();
            txtLoading.Visible = false;
        }

        private string username;
        private string rank="KH";
        private string rankname="KH";
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            if (cbxLoaiID.SelectedIndex == 1)
            {
                TaiKhoanDTO dtou = new TaiKhoanDTO();
                dtou.ID = txtID.Text;
                dtou.Pass = txtPW.Text;
                TaiKhoanBUS busu = new TaiKhoanBUS();
                dt = busu.GetID(dtou);
            }
            else if (cbxLoaiID.SelectedIndex == 0)
            {
                ThanhVienDTO dtou = new ThanhVienDTO();
                dtou.ID = txtID.Text;
                dtou.Pass = txtPW.Text;
                ThanhVienBUS busu = new ThanhVienBUS();
                dt = busu.GetID(dtou);
            }

            if (dt.Rows.Count == 1)
            {
                if (cbxLoaiID.SelectedIndex == 1) { username = dt.Rows[0]["TenNV"].ToString(); rank = dt.Rows[0]["MaCV"].ToString(); rankname = dt.Rows[0]["TenCV"].ToString(); }
                else if (cbxLoaiID.SelectedIndex == 0) { username = dt.Rows[0]["TenThanhVien"].ToString(); rank = dt.Rows[0]["LoaiThanhVien"].ToString(); rankname = rank; }
                btDangKi.Visible = false;
                btDangNhap.Visible = false;
                lbXinchao.Visible = true;
                lbXinchao.Text = "Xin chào, " + username + " [Thoát]";
                btDangNhap_Click(e, new EventArgs());
                Auth();
            }
            else
            {
                MessageBox.Show("Đăng nhập thất bại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void txtID_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                pictureBox1_Click(e, new EventArgs());
        }

        private void btDangKi_Click(object sender, EventArgs e)
        {

        }
        private void Auth()
        {
            txtLoading.Visible = true;
            txtLoading.Text = "Đang tải lại menu...";
            Application.DoEvents();
            lbPhanQuyen.Text = rankname;
            for (int i = 1; i < 11; i++)
                panel1.Controls.Find("nav" + i.ToString(), false)[0].Visible = false;
            if (rank == "AD")
            {
                for (int i = 1; i < 11; i++)
                    panel1.Controls.Find("nav"+i.ToString(), false)[0].Visible = true;
            }
            else if(rank == "TBP")
            {
                nav6.Visible = true;
                nav7.Visible = true;
            }
            else if (rank == "QL")
            {
                nav4.Visible = true;
                nav5.Visible = true;
            }
            else if (rank == "NV")
            {
                nav2.Visible = true;
                nav3.Visible = true;
            }
            else
            {
                nav1.Visible = true;
                lbPhanQuyen.Text = "KHÁCH HÀNG";
            }
            txtLoading.Visible = false;
        }       
    }
}
